package ia;

import java.awt.Graphics;
import java.io.Serializable;
import java.util.ArrayList;

import utils.Vector2d;

import graphics.DebugDrawable;

public class NodeMap implements DebugDrawable, Serializable {
	private static final long serialVersionUID = 1L;
	private ArrayList<Checkpoint> checks;
	private ArrayList<Link> links;
	
	
	public NodeMap() {
		checks = new ArrayList<Checkpoint>();
		links = new ArrayList<Link>();
	}
	
	
	public ArrayList<Checkpoint> getCheckpoints() {
		return checks;
	}
	
	
	public ArrayList<Link> getLinks() {
		return links;
	}
	
	
	public Checkpoint addCheckpoint(float x, float y) {
		Checkpoint newCP = new Checkpoint(x, y);
		checks.add(newCP);
		return newCP;
	}
	
	
	public void addCheckpoint(Checkpoint cp) {
		if(cp != null)
			checks.add(cp);
	}
	
	
	public Link link(Checkpoint cp1, Checkpoint cp2) {
		Link newLink = new Link();
		
		newLink.addCheckpoint(cp1);
		newLink.addCheckpoint(cp2);
		
		cp1.addLink(newLink);
		cp2.addLink(newLink);
		
		links.add(newLink);
		
		return newLink;
	}
	
	
	public void drawDebug(Graphics g) {
		for(Link link : links)
			link.drawDebug(g);
		
		for(Checkpoint check : checks)
			check.drawDebug(g);
	}
	
	
	public Checkpoint closestCheckpoint(Vector2d v) {
		float closest = Float.MAX_VALUE;
		Checkpoint closestCp = null;
		
		for(Checkpoint cp : checks) {
			float tmpDist = v.distance(cp.coord);
			
			if(tmpDist < closest) {
				closest = tmpDist;
				closestCp = cp;
			}
		}
		
		return closestCp;
	}
}
